package com.songyang.yougnsql.core.utils;
/**
 * @ClassName Address
 * @Description 地址相关工具
 * @date 2022/12/15 12:24
 * @Author yanceysong
 * @Version 1.0
 */
public class Address {
    /**
     * uid就是页面+偏移量，因为uid是long八个字节，
     * 页面是int四个字节，偏移量是两个字节，
     * 所以uid的前四个字节存的页号，后四个存的是偏移量，
     * 定位一个文件就是页面+偏移量所以设计为uid
     *
     * @param pageNumber 页号
     * @param offset     偏移量
     * @return uid
     */
    public static long addressToUid(int pageNumber, short offset) {
        return (long) pageNumber << 32 | (long) offset;
    }

    /**
     * 通过uid获取offset
     *
     * @param uid uid
     * @return offset
     */
    public static short getOffsetByUid(long uid) {
        return (short) (uid & ((1L << 16) - 1));
    }

    /**
     * 通过uid获取页面号
     * @param uid uid
     * @return 页面号
     */
    public static int getPageNumberByUid(long uid) {
        uid >>>= 32;
        return (int) (uid & ((1L << 32) - 1));
    }
}
